#!/usr/bin/env python
# Author:XXX

import json
import jsontree
import os
import random

class AutoVivification(dict):
    """Implementation of perl's autovivification feature."""
    def __getitem__(self, item):
        try:
            return dict.__getitem__(self, item)
        except KeyError:
            value = self[item] = type(self)()
            return value

def get_file_names(file_dir):
    for root, dirs, files in os.walk(file_dir):
        return files

def load_data(data_item, tuple_list, data_dir, file_name, subsystem_pattern):
    one_level_key=""
    two_level_key=""
    keywords_list=[]
    module_list=[]
    #tuple_list=[]
    #data_item=AutoVivification()
    with open(os.path.join(data_dir, file_name), 'r') as f:
        lines = f.readlines()
        for idx in range(len(lines)):
            ## Main system title ##
            if idx == 0:
                one_level_key = lines[0].replace("系统","").replace("\n","")
                keywords_list.append(one_level_key)
            else:
                line = lines[idx].split("\t")[0]
                if idx == len(lines):
                    data_item['root'][keywords_list[0]][two_level_key] = module_list

                ## Subsystem title ##
                if line[0:3] in subsystem_pattern:
                    if two_level_key != "":
                        data_item['root'][keywords_list[0]][two_level_key] = module_list
                    two_level_key = line.replace(keywords_list[0],"").replace("子系统","").replace("“","").replace("”","").replace("\n","")[3:]
                    keywords_list.append(two_level_key)
                    module_list=[]
                else:
                    ## Module item ##
                    module_key=line.replace(keywords_list[0],"").replace(keywords_list[-1],"")
                    module_list.append(module_key)
                    tuple_item=tuple((one_level_key, two_level_key, module_key))
                    tuple_list.append(tuple_item)
    return data_item, tuple_list

def generate_tree_and_tuple(data_dir, out_filename, subsystem_pattern):
    file_name_list = get_file_names(data_dir)
    data_item=AutoVivification()
    tuple_list=[]
    for file_name in file_name_list:
        data_item, tuple_list = load_data(data_item, tuple_list, data_dir, file_name, subsystem_pattern)
    #print(data_item)
    # Save Tree
    fp = open(out_filename, "w")
    #jsontree.dump(data_item, fp, ensure_ascii=False, indent=1)
    json.dump(data_item, fp, ensure_ascii=False, indent=1)
    #print(tuple_list)
    return data_item, tuple_list  

# TODO
def calc_tuple_sim(tuple_1, tuple_2):
    return random.uniform(10, 20)

def get_top_N_path(db_tuple_list, test_db_tuple_list):
    max_sim_db_path=""
    max_sim_test_path=""
    max_sim_value=0
    for i in range(len(db_tuple_list)):
        for j in range(len(test_db_tuple_list)):
            sim_value = calc_tuple_sim(db_tuple_list[i], test_db_tuple_list[j])
            if sim_value > max_sim_value:
                max_sim_db_path=db_tuple_list[i]
                max_sim_test_path=test_db_tuple_list[j]
                max_sim_value=sim_value
    print("Top similar path: %s and %s; The similarity is: %s" % (max_sim_db_path, max_sim_test_path, str(max_sim_value)))

if __name__ == "__main__":
    # Constant Definition
    data_dir="./data/db"
    test_data_dir="./data/test_db"
    out_filename="tree.txt"
    test_out_filename="test_tree.txt"
    subsystem_pattern=['（一）','（二）','（三）','（四）','（五）','（六）','（七）']

    db_item, db_tuple_list = generate_tree_and_tuple(data_dir, out_filename, subsystem_pattern)    
    test_db_item, test_db_tuple_list = generate_tree_and_tuple(test_data_dir, test_out_filename, subsystem_pattern)    

    get_top_N_path(db_tuple_list, test_db_tuple_list)
    ## Load Data
    #file_name_list = get_file_names(data_dir)
    #data_item=AutoVivification()
    #tuple_list=[]
    #for file_name in file_name_list:
    #    data_item, tuple_list = load_data(data_item, tuple_list, data_dir, file_name, subsystem_pattern)
    ##print(data_item)
    ## Save Tree
    #fp = open(out_filename, "w")
    ##jsontree.dump(data_item, fp, ensure_ascii=False, indent=1)
    #json.dump(data_item, fp, ensure_ascii=False, indent=1)
    #print(tuple_list)

    # ==============================================
    # ============= Test Data Step ==================
    #test_data_dir="./data/test_db"
    #test_out_filename="test_tree.txt"
    
    

